//
//  TestViewController.m
//  WuLi
//
//  Created by Gollum on 16/8/30.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "TestViewController.h"
#import "TestTableViewCell.h"
#import "ConstellationViewController.h"
#import "RankAttached.h"
#import "RankData.h"
#import "RankModel.h"
#import "RankNewscoverinfo.h"
#import "RankNewsimageinfolist.h"
#import "RankParamap.h"
#import "NetworkTool.h"
#import "TestDetailViewController.h"
#import "UIImageView+WebCache.h"
#define kTestTableViewCell @"Test"
@interface TestViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)RankModel *testData;
@property (nonatomic,strong)NSMutableArray *containerArray;
@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  使用排行榜的model
     */
    _containerArray = [NSMutableArray array];
    _testData = [[RankModel alloc] init];
    [self handleData];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/256.0 green:arc4random_uniform(255)/256.0 blue:arc4random_uniform(255)/256.0 alpha:1];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 48)];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 190;
    [self.view addSubview: _tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"TestTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTestTableViewCell];
    //创建headView
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    UIButton *headButton = [UIButton buttonWithType:UIButtonTypeCustom];
    headButton.frame = CGRectMake(20, 20, SCREEN_WIDTH - 40, 44);
    [headButton setBackgroundImage:[UIImage imageNamed:@"星座测试"] forState:UIControlStateNormal];
    [headView addSubview:headButton];
    [headButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    _tableView.tableHeaderView = headView;
    
}


- (void)handleData{

    NSString *urlString = @"http://211.151.51.100/v1/interact/message/list?pageSize=10&cursor=-1&type=DOWN";
    
    NSDictionary *header = @{@"User-Agent":@"Dalvik/2.1.0 (Linux; U; Android 5.0; ASUS_Z00ADB Build/LRX21V) Wuli/2.3.1 (agent:s;channel:wandoujia;credential:e1MyMDEwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDAyMTMyOTYzLTJlZjRjNThjZDJ9QDE0NzExODgxOTgzODY=;deviceId:357872061714383;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:5.0;idfa:null)", @"Host":@"api.5wuli.com"};
    
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        //        NSLog(@"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%@",result);
        _testData = [RankModel modelWithDic:result];
        
        [self.containerArray addObject:_testData];
        
        [self.tableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];

}
- (void)clickButton:(UIButton *)button{

    ConstellationViewController *constellation = [[ConstellationViewController alloc] init];
    [self presentViewController:constellation animated:YES completion:^{
        
    }];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;


}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _testData.data.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTestTableViewCell];
    cell.title.text = _testData.data[indexPath.section].title;
    NSURL *url = [NSURL URLWithString:_testData.data[indexPath.section].newsImageInfoList[0].url];
    [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];

    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    TestDetailViewController *testDetail = [[TestDetailViewController alloc] init];
    //判断传值类型
    if ([_testData.data[indexPath.section].layoutType isEqualToString:@"TOPIC"]) {
        testDetail.detailString = _testData.data[indexPath.section].shareUrlV2;
    }
    else if([_testData.data[indexPath.section].layoutType isEqualToString:@"QUIZ"]){
    
        testDetail.detailString = _testData.data[indexPath.section].shareUrlV2;
    }
    
    [self presentViewController:testDetail animated:YES completion:^{
        
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 10;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
